package com.example.controller;

import com.example.pojo.*;
import com.example.service.DrugCompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/drugCompany")
public class DrugCompanyController {

    @Autowired
    private DrugCompanyService drugCompanyService;

    @RequestMapping("/getList")
    public Result getList(){
        return drugCompanyService.getList();
    }
    /**
     * 医药公司分页、条件查询（2025.1.8检）
     * @param conditionsQuery
     * @return
     */
    @RequestMapping("/getCompanyList")
    public Result getCompanyList(@RequestBody ConditionsQuery conditionsQuery){

        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        //System.err.println(doctorQuery);
        List<DrugCompany> list=drugCompanyService.getCompanyList(conditionsQuery);
        // System.err.println(list);

        PageInfo<DrugCompany> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);

    }

    /**
     * 医药公司添加（2025.1.8检）
     * @param drugCompany
     * @return
     */
    @RequestMapping("/add")
    public Result addDrugCompany(@RequestBody DrugCompany drugCompany){
        return drugCompanyService.addDrugCompany(drugCompany);
    }

    /**
     * 医药公司修改（2025.1.8修正）
     * @param drugCompany
     * @return
     */
    @RequestMapping("/edit")
    public Result editDrugCompany(@RequestBody DrugCompany drugCompany){
        return drugCompanyService.editDrugCompany(drugCompany);
    }

    /**
     * 医药公司单个删除（2025.1.8检）
     * {
     *     "id":1232
     * }
     * @param map
     * @return
     */
    @RequestMapping("/delById")
    public Result delById(@RequestBody Map<String,Integer> map){

        int id =map.get("id");
       // System.err.println(id);
        return drugCompanyService.delById(id);
    }

    /**
     * 医药公司批量删除（2025.1.8修正）
     * @param map
     * @return
     */
    @RequestMapping("/delBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");

        return drugCompanyService.delBatch(ids);
    }

    /**
     *公司政策路由
     */
    /**
     * 获取公司政策分页，条件查询（2025.1.8检）
     * @param conditionsQuery
     * @return
     */
    @RequestMapping("/getCompanyPolicys")
    public Result getCompanyPolicyList(@RequestBody ConditionsQuery conditionsQuery) {
        PageHelper.startPage(conditionsQuery.getPageNum(), conditionsQuery.getPageSize());

        //System.err.println(doctorQuery);
        List<CompanyPolicy> list = drugCompanyService.getCompanyPolicyList(conditionsQuery);
        // System.err.println(list);

        PageInfo<CompanyPolicy> pageInfo = new PageInfo<>(list);

        Map<String, Object> resultMap = new HashMap<>();

        //当前页数据
        resultMap.put("list", pageInfo.getList());

        //数据总数
        resultMap.put("total", pageInfo.getTotal());

        return new Result(200, "数据加载成功", resultMap);
    }

    @RequestMapping("/addCompanyPolicy")
    public Result addCompanyPolicy(@RequestBody CompanyPolicy companyPolicy){
        return drugCompanyService.addCompanyPolicy(companyPolicy);
    }
    @RequestMapping("/delCompanyPolicyById")
    public Result delCompanyPolicyById(@RequestBody Map<String,Integer> map){

        int id =map.get("id");
        return drugCompanyService.delCompanyPolicyById(id);
    }
    @RequestMapping("/delCompanyPolicyBatch")
    public Result delCompanyPolicyBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");
        return drugCompanyService.delCompanyPolicyBatch(ids);
    }
    @RequestMapping("/editCompanyPolicy")
    public Result editCompanyPolicy(@RequestBody CompanyPolicy companyPolicy){
        return drugCompanyService.editCompanyPolicy(companyPolicy);
    }
}
